/**
 * 
 *  @project Air Traff Monitor
 * 
 *  @author M.Emre ATASEVER <m.emre.atas@gmail.com>
 *  @author M.Burak ALKAN <mburakalkan@gmail.com>
 *  @author H.Burak KARAMIK <bkaramik@gmail.com>
 *
 *	`Air Traffic Monitor' is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "mwnd.h"

#include <QTime>
#include <QHeaderView>
#include <QMenuBar>
#include <QApplication>
#include <QSettings>
#include <QCloseEvent>
#include <QMessageBox>

MainWindow::MainWindow(QKismetSocket *sock) : aquit(this), aaqt(this), aabout(this), aconntoh(this)
{
	socket	= sock;

	setAttribute(Qt::WA_DeleteOnClose,true);

	tv	= new QKismetNetView(sock);
	vtb	= new QKismetViewToolBar(tv);
	ald	= new QKismetAlertDock(sock); 
	statd	= new QKismetStatusDock(sock); 
	gra	= new QKismetGraphDock(sock);
	info	= new QKismetInfoDock(sock);


	readSettings();
	setWindowTitle("Air Traffic Monitor");
	setCentralWidget(tv);
	addToolBar(&tb);
	addToolBar(&pt);
	addToolBar(vtb);

	addDockWidget(Qt::BottomDockWidgetArea,ald);
	addDockWidget(Qt::BottomDockWidgetArea,statd);
	addDockWidget(Qt::BottomDockWidgetArea,gra);
	addDockWidget(Qt::RightDockWidgetArea,info);

	pt.addAction(ald->toggleViewAction());	
	pt.addAction(statd->toggleViewAction());
	pt.addAction(gra->toggleViewAction());
	pt.addAction(info->toggleViewAction());
	
	statusBar()->addWidget(&lstate);
	statusBar()->addWidget(&ltime);
	statusBar()->addWidget(&lnets);

	mfile.setTitle("File");
	mfile.addSeparator();
	mfile.addAction(tb.start);

	aconntoh.setText("Connect to host...");
	connect(&aconntoh,SIGNAL(triggered()),this,SLOT(connectToHost()));

	mfile.addAction(&aconntoh);

	mfile.addAction(tb.stop);
	mfile.addSeparator();
	mfile.addAction(tb.pause);	
	mfile.addAction(tb.resume);
	mfile.addSeparator();


	aquit.setText("Quit");
	connect(&aquit,SIGNAL(triggered()),this,SLOT(close()));
	aquit.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));

	
	mfile.addAction(&aquit);
	menuBar()->addMenu(&mfile);

	mview.setTitle("View");
	mview.addAction(ald->toggleViewAction());
	mview.addAction(statd->toggleViewAction());
	mview.addAction(gra->toggleViewAction());
	mview.addAction(info->toggleViewAction());
	mview.addSeparator();
	mview.addAction(tb.toggleViewAction());
	mview.addAction(pt.toggleViewAction());
	mview.addAction(vtb->toggleViewAction());

	menuBar()->addMenu(&mview);

	connect(tb.stop,SIGNAL(triggered()),sock,SLOT(Disconnect()));
	connect(tb.start,SIGNAL(triggered()),sock,SLOT(Connect()));
	connect(tb.pause,SIGNAL(triggered()),sock,SLOT(Pause()));
	connect(tb.resume,SIGNAL(triggered()),sock,SLOT(Resume()));
	connect(sock,SIGNAL(serverTime(int)),this,SLOT(showTime(int)));
	connect(sock,SIGNAL(infoInfo(iInfo)),this,SLOT(showInfo(iInfo)));
}
MainWindow::~MainWindow()
{
	delete tv;
	delete vtb;
	delete ald;
	delete statd;
}
void MainWindow::showTime(int time)
{
	QTime t;
	t = t.addSecs(time-stime);
	ltime.setText("Elapsed: " + t.toString("hh:mm:ss"));
}

void MainWindow::showInfo(iInfo data)
{
	lnets.setText("Networks: "+data.props[0].toString()+", Packets: "+data.props[1].toString()+", Crypted: "+data.props[2].toString());
}

void MainWindow::setServerName(QString name)
{
	servname = name;
	setWindowTitle("qKismet ["+name+"]");
}

void MainWindow::setSTime(int stime)
{
	this->stime = stime;
}

void MainWindow::showState(QAbstractSocket::SocketState s)
{
	QString c;
	switch(s)
	{
		case 0: c = "Disconnected";
			tb.stop->setEnabled(false);
			tb.pause->setEnabled(false);
			tb.resume->setEnabled(false);
			tb.start->setEnabled(true);
			aconntoh.setEnabled(true);
			break;
		case 1: c = "Looking up host";
			break;
		case 2: c = "Connecting";
			break;
		case 3: c = "Connected";
			break;
		case 6: c = "Closing connection";
			break;
		default: c = "";
	}
	if(s>1)
	{
		tb.stop->setEnabled(true);
		tb.pause->setEnabled(true);
		tb.resume->setEnabled(true);
		tb.start->setEnabled(false);
		aconntoh.setEnabled(false);
	}
	lstate.setText(c);
}

void MainWindow::readSettings()
{
	QSettings settings("qkismet", "qkismet");

	settings.beginGroup("MainWindow");
	resize(settings.value("size", QSize(400, 400)).toSize());
	move(settings.value("pos", QPoint(200, 200)).toPoint());
	settings.endGroup();
}

void MainWindow::writeSettings()
{
	QSettings settings("qkismet", "qkismet");
	settings.beginGroup("MainWindow");
	settings.setValue("size", size());
	settings.setValue("pos", pos());
	settings.endGroup();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	writeSettings();
	event->accept();
}

void MainWindow::connectToHost()
{
	HostDialog dial(this);
	if(dial.exec())
		socket->Connect(dial.getHost(),dial.getPort());
}

HostDialog::HostDialog(QWidget *parent) : QDialog(parent)
{
	setWindowTitle("Connect to host");

	layoutmain	= new QVBoxLayout();
	setLayout(layoutmain);
	layouted	= new QHBoxLayout();
	layoutmain->addLayout(layouted);

	lhost.setText("Host: ");
	layouted->addWidget(&lhost);
	layouted->addWidget(&edhost);

	lport.setText("Port: ");
	layouted->addWidget(&lport);
	
	edport.setMinimum(1);
	edport.setMaximum(0xffff);
	edport.setValue(2501);

	layouted->addWidget(&edport);

	ok.setText("OK");
	ok.setDefault(true);
	cancel.setText("Cancel");

	connect(&ok,SIGNAL(clicked()),this,SLOT(accept()));
	connect(&cancel,SIGNAL(clicked()),this,SLOT(reject()));

	layoutbut	= new QHBoxLayout();
	layoutmain->addLayout(layoutbut);
	
	layoutbut->addStretch();
	layoutbut->addWidget(&ok);
	layoutbut->addWidget(&cancel);
}
QString HostDialog::getHost()
{
	return edhost.text();
}
int HostDialog::getPort()
{
	return edport.value();
}
HostDialog::~HostDialog()
{
	delete layouted;
	delete layoutbut;
	delete layoutmain;
}
